package com.lgx.common.web.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 
 * @author acer
 *
 */
public class FileUtils {
	
	public static final String FILE_UPLOAD_PATH = "upload";
	
	public static final String FILE_IP_PATH = "ip";
	
	public static final int UPLOAD_MAX_SIZE = 100 * 1024 * 1024;
	
	/**
	 * 获得上传文件存放的目录，上传目录为upload，每个月新建一个文件夹，文件夹格式2011-11
	 * 
	 * @return
	 */
	public static String ipPath() 
	{
		File file = new File(FileUtils.class.getClassLoader().getResource("/")
				.getPath());
		file = file.getParentFile().getParentFile();
		file = new File(file.getAbsolutePath()
				+ System.getProperty("file.separator") + FileUtils.FILE_IP_PATH);
		if (!file.exists()) {
			file.mkdir();
		}
		return file.getAbsolutePath();
	}
	
	/**
	 * 获得上传文件存放的目录，上传目录为upload，每个月新建一个文件夹，文件夹格式2011-11
	 * 
	 * @return
	 */
	public static String uploadPath() 
	{
		File file = new File(FileUtils.class.getClassLoader().getResource("/")
				.getPath());
		file = file.getParentFile().getParentFile();
		file = new File(file.getAbsolutePath()
				+ System.getProperty("file.separator") + FileUtils.FILE_UPLOAD_PATH);
		if (!file.exists()) {
			file.mkdir();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar c = Calendar.getInstance();
		String directoryName = sdf.format(c.getTime());
		file = new File(file.getAbsoluteFile()
				+ System.getProperty("file.separator") + directoryName);
		if (!file.exists()) {
			file.mkdir();
		}
		return file.getAbsolutePath();
	}
	

	/**
	 * 获得上传文件存放的目录，上传目录为upload，每个月新建一个文件夹，文件夹格式2011-11
	 * 
	 * @return
	 */
	public static String filePath() 
	{
		String filePath =  FileUtils.FILE_UPLOAD_PATH + System.getProperty("file.separator");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar c = Calendar.getInstance();
		String directoryName = sdf.format(c.getTime());
		return filePath + directoryName;
	}
	
	/**
	 * 
	 * @param filePath
	 */
	public static void fileDelete(String filePath)
	{
		File file = new File(FileUtils.class.getClassLoader().getResource("/")
				.getPath());
		file = file.getParentFile().getParentFile();
		file = new File(file.getAbsolutePath()
				+ System.getProperty("file.separator") + filePath);
		if (file.exists()) {
			file.delete();
		}
	}
}
